using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using Pixysoft.Web;
using Pixysoft.Framework.Verifications;

namespace Pixysoft.Framework.Comet
{
    class StringHelper
    {
        public const string global_framework_id = "pixysoft.framework.comet";

        public const long contract_framework_id = 20100504001001;

        public const string default_contenttype = "application/octet-stream";


        public const string default_schemafilename = "pixysoft.framework.comet.message.schema.config";

        public const string default_dbname = "pixysoft.framework.comet.message.db";



        public const string default_session_sendercode = ".pixysoft.framework.comet.sendercode";

        public const string default_querystring_sendercode = "sendercode";
        public const string default_querystring_key = "key";


        public const int default_status_succeed = 1;

        public const int default_status_fail = 0;


        public static string GetSenderCode(HttpContext context)
        {
            string sendercode = SessionHelper.Instance.GetSession<string>(StringHelper.default_session_sendercode);

            if (!string.IsNullOrEmpty(sendercode))
                return sendercode;

            sendercode = context.Request.QueryString[StringHelper.default_querystring_sendercode];
            string key = context.Request.QueryString[StringHelper.default_querystring_key];

            if (!Verification.Verifies(sendercode, key).IsNotNullOrEmpty().Result())
            {
                return null;
            }

            if (!CometManager.Login(sendercode).Equals(key))
            {
                return null;
            }

            SessionHelper.Instance.AddSession(StringHelper.default_session_sendercode, sendercode);
            return sendercode;
        }

        public static string GetMessageCode()
        {
            return Pixysoft.Tools.GlobalTimer.Instance.GetUniqueStringSequence(3);
        }

        public static long GetTimestamp()
        {
            return Pixysoft.Tools.GlobalTimer.Instance.GetUniqueSequence();
        }

        public static void SetCometResponse()
        {
        }
    }

}
